import { Toast } from 'antd-mobile';
import pathToRegexp from 'path-to-regexp';
import { Route } from 'dva/router';
import {
    storeApi,
    storegoodsApi,
    storeNewGoodsApi,
    storeCounponApi,
    storeClassApi,
    storeClassGoodsApi,
    storeFavgoodsApi,
    storeCouApi,
    classGoodsApi,
    filterGoodsApi,
    filterSureApi,
    OrderSearchApi,
} from './api';
import {goodsSearch} from "../search/api";
import {parse} from "query-string";




export default {
    namespace: 'storesShow',
    state: {
        check:"",
        pageNo:1,
        filter:{
            searchType:"",
            keyword:"",
            pageNo:1,
            brandId:"",
            areaId:"",
            specFilter:"",
            sortField:"",
            orderBy:'asc',
            //sortOrder:'asc',
            identification:"1",
            storeId:"",
            isFav:'',
        },
        specList:[],
        minimumPrice:'',
        maximumPrice:'',
        //classGoods:''
    },
    reducers:{
        save(state, { data }) {
            return { ...state, ...data };
        },
        changeSpecCheck(state, { index, sonIndex, check }){
            let arr =  state.specList.map((v,i,a) => {
                if(i==index){
                    let newSpecList = v.specValueList.map((vv,ii,aa) => {
                        if(sonIndex==ii){
                            return { ...vv, check: check }
                        }else {
                            return vv
                        }
                    })
                    return { ...v, specValueList:newSpecList };
                }else {
                    return v
                }
            })

            return { ...state, specList:arr }
        }
    },
    effects: {
*getstoresEFF({obj}, { put, call }) {
   // console.log(obj)
    // const goodsearch = yield select((state)=>state.goodsearch);
    // const specList = goodsearch.specList;
    // const specValueIds = [];
    // const specbandId = [];
    const result = yield call(storeApi,obj);
            console.log(result)
            if (result.code == 200) {
                yield put({ type: 'save', data: result.ret });
                yield put({ type: 'save', isFav: result.isFav });

                Toast.hide();
            } else {

            }
        },
    //店铺全部商品
    *getgoodsEFF({obj}, { put, call, select }) {
       // console.log(obj)
        const storesShow = yield select((state)=>state.storesShow);
        const result = yield call(storegoodsApi, {...storesShow.filter, ...obj});
      console.log(result, {...filter, ...obj}, "请求。。。")
        if (result.code == 200) {
            console.log(result.ret);
            if(result.ret.pageNo <= 1) yield put({ type: 'save', data: result.ret });
            else yield put({ type: 'save', data: {...result.ret, list: [...storesShow.list, ...result.ret.list]} });
            Toast.hide();
        } else {

        }
        return {r: true};
    },
        //店铺上新
        *getnewgoodsEFF({obj}, { put, call }) {
            // console.log(obj)
            const result = yield call(storeNewGoodsApi,obj);
            console.log(result)
            if (result.code == 200) {
                yield put({ type: 'save', data:{newList:result.ret.list}});
                Toast.success(result.msg, 1.5);
            } else {

            }
        },
        //获取优惠券
        *getcouponEFF({obj}, { put, call }) {
            // console.log(obj)
            const result = yield call(storeCounponApi,obj);
            console.log(result)
            if (result.code == 200) {
                yield put({ type: 'save', data:{total:result.ret.list}});
                Toast.hide();
            } else {

            }
        },
        //领取优惠券
        *getcouEFF({obj},{put,call}){
                const result = yield call(storeCouApi,obj);
            console.log(result)
            if (result.code == 200) {
                Toast.success(result.msg, 1.5);
                yield put({ type: 'save', data:{status: result.msg}});
            } else {

            }
            return result;
        },
        //关注店铺
        *getattEFF({obj},{put,call}){
            const result = yield call(storeFavgoodsApi,obj);
            console.log(result)
            if (result.code == 200) {
               // yield put({type: 'save', data: {status: result.msg}});

                yield put({type: 'save', data: {isFav:result.msg=='收藏成功'?1:0 }});
                Toast.success(result.msg, 1.5);
            }else {
            }
            return result;
        },
        //获取店铺下所有分类
        *getclassEFF({obj},{put,call}){
            const result = yield call(storeClassApi,obj);
            console.log(result)
            if (result.code == 200) {
                yield put({type: 'save', data: {chart: result.ret.list}})
                //yield put({type: 'save', data: {isShow: !show}});
               // Toast.success(result.msg, 1.5);
            }else {

            }
        },
        //获取店铺各个分类的商品
        *classGoodsEFF({obj},{put,call}){
             console.log(obj)
            const result = yield call(storeClassGoodsApi,obj);
            if (result.code == 200) {
            //    console.log(result.ret.list,'6fyuffjhg')
               // yield put({type: 'save', data: {classGoods: result.ret.list}})
              //  console.log(result)
                // Toast.success(result.msg, 1.5);
            }else {
                Toast.fail(result.msg, 1.5);
            }
        },
        //获取分类商品下的筛选
        *filterGoodsEFF({obj},{put,call}){
           // console.log(obj)
            const result = yield call(filterGoodsApi,obj);
            if (result.code == 200) {
                yield put({type: 'save', data: {specList: result.ret.specList}})
               // console.log(filterGoods)
                // Toast.success(result.msg, 1.5);
            }else {

            }
        },
        // 全部订单搜索页面
        *OrderSearchEFF({obj},{put,call,select}){
            const storesShow = yield select((state)=>state.storesShow);
            const result = yield call(OrderSearchApi,{check:storesShow.check,pageNo:storesShow.pageNo,...obj});
            console.log(result,"result")
            if (result.code == 200) {
                if(result.ret!=""){
                    yield put({type: 'save', data: result.ret})
                    if(result.ret.pageNo <= 1) yield put({ type: 'save', data: { list: result.ret.list} });
                    else yield put({ type: 'save', data: {list: [...storesShow.list, ...result.ret.list] } });
                    return { r:true };
                }
            }else {

            }
        },

        //---------------------------------------------------------------
        *filterSureEFF({ obj }, { put, call, select }) {
            //Toast.loading('加载中...', 3);
            const storesShow = yield select((state)=>state.storesShow);
            const specList = storesShow.specList;
            console.log(specList)
            const specValueIds = [];
            const specbandId = [];
            console.log(storesShow)
            for(let i=0; i<specList.length; i++){
                const specValueIdItem = [];
                for(let j=0; j<specList[i].specValueList.length; j++){
                    const specValueListItem = specList[i].specValueList[j];
                    if(specValueListItem.check==true){
                        if(specValueListItem.isBrand==0){
                            specValueIdItem.push(specValueListItem.id)
                        }else if(specValueListItem.isBrand==1){
                            specbandId.push(specValueListItem.id)
                        }
                    }
                }
                if (specValueIdItem.length > 0) {
                    specValueIds.push(specValueIdItem.join(":"));
                }
            }

            //console.log(specValueIds,specbandId);
            //console.log(storesShow.filter)

            const result = yield call(filterSureApi, { ...storesShow.filter, ...obj, minimumPrice:storesShow.minimumPrice, maximumPrice:storesShow.maximumPrice, brandId:specbandId.join(","), specFilter:specValueIds.join(",") });
            if ( result.code == 200 ) {
              console.log(storesShow.filter)
                Toast.hide();
                if( result.ret != '' ){
                    if( obj.pageNo !==undefined && obj.pageNo == 1 ){
                        yield put({ type: 'save', data: { ...result.ret, classGoods:[ ...result.ret.list ], hasMore: result.ret.list.length==20, filter:{ ...storesShow.filter, ...obj } } });
                    }else {
                        yield put({ type: 'save', data: { ...result.ret, classGoods:[ ...storesShow.list, ...result.ret.list ], hasMore: result.ret.list.length==20, filter:{ ...storesShow.filter, ...obj } } });
                        }
                }else {
                    yield put({ type: 'save', data: { hasMore: false ,classGoods:[], filter:{ ...storesShow.filter, ...obj }  }});
                }
                return { r:true };
            } else {
                yield put({ type: 'save', data: { hasMore: false ,classGoods:[], filter:{ ...storesShow.filter, ...obj }  }});
                Toast.fail(result.msg, 1.5);
            }
        },
},
    subscriptions: {
        setup({ dispatch, history }) {
            return history.listen(({ pathname, query }) => {
                if (pathname.includes('/storesSearch/')) {
                    const match = pathToRegexp('/storesSearch/:stcId/:id/:stcName').exec(pathname);
                    dispatch({ type: 'filterSureEFF',
                        obj:{
                            searchType:'keywordSearch',
                            pageNo:1,
                            storeId:match[2],
                            keyword:match[3]
                        }
                    });
                   dispatch({ type: 'getgoodsEFF',obj:{id:match[2]}});
                };
                if(pathname.includes('searchOrder')){
                    const match = pathToRegexp('/searchOrder/:data').exec(pathname);
                    dispatch({type:'OrderSearchEFF',obj:{check:match[1],pageNo:1}})
                }
            });
        },
    },
};
